package cn.xm.pet.service.impl;

import cn.xm.basic.config.BussinessException;
import cn.xm.basic.util.BaiduAuditUtils;
import cn.xm.pet.domain.Pet;
import cn.xm.pet.domain.PetDetail;
import cn.xm.pet.domain.PetOnlineAuditLog;
import cn.xm.pet.mapper.PetDetailMapper;
import cn.xm.pet.mapper.PetMapper;
import cn.xm.pet.service.PetOnlineAuditLogService;
import cn.xm.pet.service.PetService;
import cn.xm.basic.service.impl.BaseServiceImpl;
import org.apache.velocity.runtime.directive.Foreach;
import org.apache.velocity.runtime.directive.contrib.For;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xm
 * @since 2022-10-29
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements PetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogService petOnlineAuditLogService;

    @Override
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = pet.getDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.add(petDetail);
    }

    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }

    @Override
    public void onsale(List<Long> ids) {
        Date date = new Date();
        PetDetail petDetail = new PetDetail();
        List<Pet> byIds = petMapper.findByIds(ids);
        // 找到所有下架状态的宠物
        List<Pet> pets = byIds.stream().filter(pet -> pet.getState() == 0).collect(Collectors.toList());
        for (Pet pet : pets) {
            StringBuffer stringBuffer = new StringBuffer();
            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            if (pets != null) {
                if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                    stringBuffer.append("宠物名字不合规，宠物名为:" + pet.getName());
                }
                if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[0])) {
                    stringBuffer.append("宠物图片不合规，宠物图片地址:" + pet.getResources());
                } else {
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources())) {
                        stringBuffer.append("宠物图片不合规，宠物图片地址:" + pet.getResources());
                    }
                }
                if (stringBuffer.length()>0) {
                    petOnlineAuditLog.setPetId(pet.getId());
                    petOnlineAuditLog.setAuditTime(date);
                    petOnlineAuditLog.setNote(stringBuffer.toString());
                    petOnlineAuditLog.setState(0);
                    petOnlineAuditLogService.add(petOnlineAuditLog);
                    continue;
                }
                pet.setState(1);
                pet.setOnsaletime(date);
                petMapper.update(pet);
            }

        }


    }

    @Override
    public Pet findDetailByPetId(Long id) {
        return petMapper.findDetailByPetId(id);
    }


}
